<?php
/**
 * 广告管理
 */
namespace app\admins\controller\set;
use \app\admins\controller\Auth;
class Ad extends Auth{
    
    
    /**
     * 列表
     */
    public function index(){
        
        try{
            $pid = input('pid',0);
            $where = [
                'page'      => input('page',1),
                'positionid'=> $pid,
                'status'    => input('status'),
                'starttime' => input('starttime'),
                'endtime'   => input('endtime')
            ];
            
            $Adposition = new \logic\set\ad\Adposition($this->getUser());            
            $adposition = $Adposition->find(['id'=>$pid]);
            if( !$adposition ) E('广告位不存在');
            
            $Ad = new \logic\set\ad\Ad($this->getUser());
            $data = $Ad->getAdminList($where);
            $showpage = showpage($data['total'],$data['page'],$data['limit']);
            $this->assign(get_defined_vars());
            return $this->fetch();
            
        }catch(\think\Exception $e){
            $this->error( $e->getMessage());
        }
    }
    
    /**
     * 添加
     */
    public function add(){
        $pid = input('pid');        
        try{
            if( !$pid ) E('广告位参数不能为空');
            if( request()->isPost() ){                
                $param = array(
                    'positionid'=> $pid,
                    'name'      => input('name'),
                    'pic'       => input('pic'),
                    'url'       => input('url'),
                    'sort'      => input('sort'),
                    'des'       => input('des'),
                    'starttime' => input('starttime'),
                    'endtime'   => input('endtime')
                );                
                $Ad = new \logic\set\ad\Ad($this->getUser());
                $Ad->add($param);
                return $this->success('添加成功','/set/ad/index/pid/'.$pid);
            }            
            $Adposition = new \logic\set\ad\Adposition($this->getUser());            
            $adposition = $Adposition->find(['id'=>$pid]);
            if( !$adposition ) E('广告位不存在');
            if( $adposition['status'] != 1 ) E('广告位已经关闭');
            $this->assign(get_defined_vars());
            return $this->fetch();
        }catch( \think\Exception $e){
            $this->error($e->getMessage());
        }
        
        
    }
    
    /**
     * 修改
     */
    public function edit(){
        try{
            $id = input('id');
            if( request()->isPost() ){                   
                $param = array(
                    'name'      => input('name'),
                    'pic'       => input('pic'),
                    'url'       => input('url'),
                    'sort'      => input('sort'),
                    'des'       => input('des'),
                    'starttime' => input('starttime'),
                    'endtime'   => input('endtime'),
                    'status'    => input('status')
                );                
                $Ad = new \logic\set\ad\Ad($this->getUser());
                $Ad->edit($id,$param);
                $data =  $Ad->find(['id'=>$id]);
                return $this->success('修改成功','/set/ad/index/pid/'.$data['positionid']);
            }
            
            $Ad = new \logic\set\ad\Ad($this->getUser());
            $data =  $Ad->find(['id'=>$id]);
            
            $Adposition = new \logic\set\ad\Adposition($this->getUser());            
            $adposition = $Adposition->find(['id'=>$data['positionid']]);
            if( !$adposition ) E('广告位不存在');
            if( $adposition['status'] != 1 ) E('广告位已经关闭');
            
            $this->assign(get_defined_vars());
            return $this->fetch();
        }catch(\think\Exception $e){
            $this->error($e->getMessage());
        }
    }
    
    /**
     * 修改状态
     */
    public function editstatus(){
        try{
            $id = input('id');
            $AdminManage = new \logic\set\ad\Ad($this->getUser());
            $result = $AdminManage->editStatus($id);
            return $this->success('成功');
        }catch(\think\Exception $e){
            return $this->error('失败');
        }
    }
}